package yesjava;

import java.io.*;

public class FileUtil {
    public static void copy(File src, File dest) {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            if (src.exists()) {
                if(dest.exists()){
                    dest.delete();
                }
                dest.createNewFile();
                in = new BufferedInputStream(new FileInputStream(src));
                out = new BufferedOutputStream(new FileOutputStream(dest));
                byte[] bytes = new byte[512];
                while (true){
                    int count = in.read(bytes);
                    if(count==-1){
                        break;
                    }
                    out.write(bytes,0,count);
                }
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {if(in!=null) in.close();} catch (IOException e) {throw new RuntimeException(e);}
            try {if(out!=null) out.close();} catch (IOException e) {throw new RuntimeException(e);}
        }
    }

    public static void copy(String src, String dest) {
        copy(new File(src),new File(dest));
    }
}
